package sq.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sq.dao.User;
import sq.service.AuthenticationService;
import sq.vo.BaseRespVO;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author sunquan
 */
@RestController
public class AuthenticationController {

    @Autowired
    private AuthenticationService authenticationService;

    /**
     * 登录接口功能实现Controller层
     *
     * @param username 开发者姓名/开发者手机号
     * @param password 开发者密码
     * @param request  request请求
     * @return 一个VO类实体
     */
    @PostMapping("/sign_in")
    public BaseRespVO<User> login(@RequestParam String username,
                                  @RequestParam String password,
                                  HttpServletRequest request) {
        BaseRespVO baseRespVO = new BaseRespVO();
        User user = null;
        try {
            user = authenticationService.loginService(username, password, request);
        } catch (NullPointerException e) {
            e.printStackTrace();
            baseRespVO.setCode("2");
            baseRespVO.setMsg("用户不存在");
            return baseRespVO;
        }
        if (user.getType().equals("admin")) {
            baseRespVO.setCode("0");
            baseRespVO.setMsg("管理员登陆成功");
            baseRespVO.setData(user);

        } else {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("用户登陆成功");
            baseRespVO.setData(user);
        }

        return baseRespVO;
    }


    /**
     * 注册还需要使用注册码
     *
     * @param username 开发者用户名
     * @param password 密码
     * @param phone    开发者手机号
     * @param vcode    短信验证码
     */
    @PostMapping("/sign_up")
    public BaseRespVO<User> auth(@RequestParam String username,
                                 @RequestParam String password,
                                 @RequestParam String phone,
                                 @RequestParam String vcode) {
        return authenticationService.auth(username, password, phone, vcode);
    }

    /**
     * 注销
     */
    @PostMapping("/sign_out")
    public BaseRespVO unAuth(HttpServletRequest request) {
        authenticationService.unAuth(request);
        return null != request.getSession() ?
                new BaseRespVO("0", "SignOut successful") :
                new BaseRespVO("1", "SignOut lose,403 or NullPointerException");
    }

    /**
     * 判断session
     *
     * @param request
     * @return
     */
    @PostMapping("/session")
    public BaseRespVO validateSession(HttpServletRequest request) {
        BaseRespVO baseRespVO = new BaseRespVO();
        if (authenticationService.sessionExists(request)) {
            baseRespVO.setCode("0");
            baseRespVO.setMsg("session存在");
        } else {
            baseRespVO.setCode("1");
            baseRespVO.setMsg("session不存在");
        }
        return baseRespVO;
    }

    @PostMapping("/user")
    public BaseRespVO returnId(HttpServletRequest request) {
        Integer userId;
        userId=authenticationService.retuenUserId(request);
        BaseRespVO baseRespVO = new BaseRespVO();
        baseRespVO.setCode("0");
        baseRespVO.setData(userId);
        baseRespVO.setMsg("session存在");
       return  baseRespVO;
    }

    @PostMapping("/user/show/{userId}")
    public BaseRespVO returnUser(HttpServletRequest request, @PathVariable(name = "userId") Integer userId) {
        BaseRespVO baseRespVO = new BaseRespVO();
        User user=authenticationService.returnUser(userId);
        baseRespVO.setCode("0");
        baseRespVO.setData(user);
        baseRespVO.setMsg("session存在");
        return  baseRespVO;
    }

}
